package com.sitech.portal.system.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.sitech.ibnms.core.JGridBaseAction;
import com.sitech.ibnms.util.JSONUtil;
import com.sitech.portal.system.domain.TblSYS_LOGINMSG;
import com.sitech.portal.system.domain.TblSYS_USER_ROLE;
import com.sitech.portal.system.form.UserPortalForm;
import com.sitech.portal.system.service.ISysModuleService;
import com.sitech.portal.system.service.ISysPanelConfigService;
import com.sitech.portal.system.service.ISysParaService;
import com.sitech.portal.system.service.ISysRolePortalService;
import com.sitech.portal.system.service.ISysUserPortalService;
import com.sitech.portal.system.service.ISysUserRoleService;
import com.sitech.portal.system.service.ISysUserShortcutService;
import com.sitech.portal.util.CommUtil;


public class UserPortalAction extends JGridBaseAction{

	private String flag;
	private List list;
	private ISysModuleService sysModuleService;
	private ISysUserPortalService sysUserPortalService;
	private ISysParaService sysParaService;
	private ISysUserRoleService sysUserRoleService;
	private ISysRolePortalService sysRolePortalService;
	private ISysPanelConfigService sysPanelConfigService;
	private ISysUserShortcutService sysUserShortcutService;
	private UserPortalForm theForm = new UserPortalForm();
	private List sysPanelConfigList;
	private List resultList;
	private String LEFT_PANEL_COLLECT;
	private String RIGHT_PANEL_COLLECT;
	private String nodeID;
	private String rootID;
	private String module_type;
	private String shortcut_name;
	private String shortcut_path;
	private String module_id;
	
	
	public ISysUserPortalService getSysUserPortalService() {
		return sysUserPortalService;
	}

	public void setSysUserPortalService(ISysUserPortalService sysUserPortalService) {
		this.sysUserPortalService = sysUserPortalService;
	}

	public ISysParaService getSysParaService() {
		return sysParaService;
	}

	public void setSysParaService(ISysParaService sysParaService) {
		this.sysParaService = sysParaService;
	}

	public ISysUserRoleService getSysUserRoleService() {
		return sysUserRoleService;
	}

	public void setSysUserRoleService(ISysUserRoleService sysUserRoleService) {
		this.sysUserRoleService = sysUserRoleService;
	}

	public ISysRolePortalService getSysRolePortalService() {
		return sysRolePortalService;
	}

	public void setSysRolePortalService(ISysRolePortalService sysRolePortalService) {
		this.sysRolePortalService = sysRolePortalService;
	}

	public ISysPanelConfigService getSysPanelConfigService() {
		return sysPanelConfigService;
	}

	public void setSysPanelConfigService(
			ISysPanelConfigService sysPanelConfigService) {
		this.sysPanelConfigService = sysPanelConfigService;
	}

	public ISysUserShortcutService getSysUserShortcutService() {
		return sysUserShortcutService;
	}

	public void setSysUserShortcutService(
			ISysUserShortcutService sysUserShortcutService) {
		this.sysUserShortcutService = sysUserShortcutService;
	}

	public UserPortalForm getTheForm() {
		return theForm;
	}

	public void setTheForm(UserPortalForm theForm) {
		this.theForm = theForm;
	}

	public List getSysPanelConfigList() {
		return sysPanelConfigList;
	}

	public void setSysPanelConfigList(List sysPanelConfigList) {
		this.sysPanelConfigList = sysPanelConfigList;
	}

	public List getResultList() {
		return resultList;
	}

	public void setResultList(List resultList) {
		this.resultList = resultList;
	}

	public String getLEFT_PANEL_COLLECT() {
		return LEFT_PANEL_COLLECT;
	}

	public void setLEFT_PANEL_COLLECT(String left_panel_collect) {
		LEFT_PANEL_COLLECT = left_panel_collect;
	}

	public String getRIGHT_PANEL_COLLECT() {
		return RIGHT_PANEL_COLLECT;
	}

	public void setRIGHT_PANEL_COLLECT(String right_panel_collect) {
		RIGHT_PANEL_COLLECT = right_panel_collect;
	}

	public String getNodeID() {
		return nodeID;
	}

	public void setNodeID(String nodeID) {
		this.nodeID = nodeID;
	}

	public String getRootID() {
		return rootID;
	}

	public void setRootID(String rootID) {
		this.rootID = rootID;
	}

	public String getModule_type() {
		return module_type;
	}

	public void setModule_type(String module_type) {
		this.module_type = module_type;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public ISysModuleService getSysModuleService() {
		return sysModuleService;
	}

	public void setSysModuleService(ISysModuleService sysModuleService) {
		this.sysModuleService = sysModuleService;
	}

	public String getShortcut_name() {
		return shortcut_name;
	}

	public void setShortcut_name(String shortcut_name) {
		this.shortcut_name = shortcut_name;
	}

	public String getShortcut_path() {
		return shortcut_path;
	}

	public void setShortcut_path(String shortcut_path) {
		this.shortcut_path = shortcut_path;
	}

	public String getModule_id() {
		return module_id;
	}

	public void setModule_id(String module_id) {
		this.module_id = module_id;
	}

	public String loadModuleListByParentId(){
		String parent_id = this.getRequest().getParameter("module_id");
        TblSYS_LOGINMSG user = this.getCurrentUser();
        list = sysModuleService.findModuleListByLoginIDAndParentId(user.getLOGIN_ID(), parent_id);
        return SUCCESS;
	}
	
	public String portal(){
		
		TblSYS_LOGINMSG user = this.getCurrentUser();
		String login_id = user.getLOGIN_ID();
		HashMap map = sysUserPortalService
				.findObjTB_SYS_USER_PORTALByLoginID(login_id);
		String PANEL_COLLECT = "0;1";
		HashMap hm = sysParaService.findSysParaByParaId("PANEL_COLLECT");
		if (hm != null && hm.get("PARA_VALUE") != null) {
			PANEL_COLLECT = hm.get("PARA_VALUE").toString();
		}
		// 如果用户配置了portal信息
		System.out.println("默认配置=== TB_SYS_PARA =PANEL_COLLECT："
				+ PANEL_COLLECT);
		if (map != null) {
			PANEL_COLLECT = map.get("PANEL_COLLECT").toString();
		} else {
			// 如果没有定制,需要给参数初始化固定显示参数.
			// PANEL_COLLECT="0;1";
		}
		// 获取个人角色内所有portal,供用户进行定制,Start------
		String allPorals = "";
		String lastPortals = "";
		String singleRole = "";
		String comboRole = "";
		// 1.获取用户所具有的系统角色
		List roleList = sysUserRoleService.findRoleListByUserId(login_id);
		// 2.获取系统内所有角色所对应的portal
		List rolePortalList = sysRolePortalService.findRolePortalList();
		String[] ROLE_PORTAL_IDS = new String[300];
		// 3.portal角色与用户所拥有的系统角色进行对比,目的找出用户角色内所有的portal
		for (int i = 0; i < rolePortalList.size(); i++) {
			int sameSum = 0;
			int maxSameSum = 0;
			HashMap rolePortalMap = (HashMap) rolePortalList.get(i);
			// 3.1portal 角色可能有类似 1022#1050
			// 的组合.为了获取角色内所有的portal,这里进行分别获取,我感觉这里可以再优化
			String ROLE_IDS = rolePortalMap.get("ROLE_IDS").toString();
			ROLE_PORTAL_IDS = ROLE_IDS.split("#");
			// 3.11 if(单角色portal)else(多角色组合portal)
			if (ROLE_PORTAL_IDS.length == 1) {
				for (int m = 0; m < roleList.size(); m++) {
					TblSYS_USER_ROLE tblSYS_USER_ROLE = (TblSYS_USER_ROLE) roleList
							.get(m);
					if (ROLE_PORTAL_IDS[0]
							.equals(tblSYS_USER_ROLE.getROLE_ID())) {
						singleRole += rolePortalMap.get("PANEL_COLLECT")
								.toString();
						sameSum++;
					}
				}
				if (sameSum > maxSameSum) {
					maxSameSum = sameSum;
				}
				// 如果用户只有一种角色,且与角色portal 相匹配,则跳出循环
				if (maxSameSum == roleList.size()) {
					break;
				}
			} else {
				for (int j = 0; j < ROLE_PORTAL_IDS.length; j++) {
					for (int m = 0; m < roleList.size(); m++) {
						TblSYS_USER_ROLE tblSYS_USER_ROLE = (TblSYS_USER_ROLE) roleList
								.get(m);
						if (ROLE_PORTAL_IDS[j].equals(tblSYS_USER_ROLE
								.getROLE_ID())) {
							sameSum++;
						}
					}
					if (sameSum > maxSameSum) {
						maxSameSum = sameSum;
						comboRole = rolePortalMap.get("PANEL_COLLECT")
								.toString();
					}
					// 如果完全匹配，则跳出循环
					if (maxSameSum == roleList.size()) {
						break;
					}
				}
				// 如果完全匹配，则跳出循环
				if (maxSameSum == roleList.size()) {
					break;
				}
			}
		}
		// 4.获取到用户角色内所有portal的组合，可能有重复的portal,需要过滤一下。
		allPorals = singleRole + "," + comboRole;
		String[] portals = allPorals.split(",");
		List list = Arrays.asList(portals);
		Set set = new HashSet(list);
		Object[] obj = (Object[]) set.toArray();
		// 5. 过滤后,封装查询个人portal参数。
		for (int k = 0; k < obj.length; k++) {
			lastPortals += "'" + obj[k].toString() + "',";
		}
		lastPortals = lastPortals.substring(0, lastPortals.length() - 1);

		List rst = sysPanelConfigService
				.findSysPanelConfigListByRole(lastPortals);
		// 6.个人定制展现
		String[] PANEL_COLLECTS = PANEL_COLLECT.split(";");
		LEFT_PANEL_COLLECT = PANEL_COLLECTS[0];
		RIGHT_PANEL_COLLECT = PANEL_COLLECTS[1];
		String[] LEFT_PANEL_COLLECTS = LEFT_PANEL_COLLECT.split(",");
		String[] RIGHT_PANEL_COLLECTS = RIGHT_PANEL_COLLECT.split(",");
		String[] PANEL_ID = new String[LEFT_PANEL_COLLECTS.length
				+ RIGHT_PANEL_COLLECTS.length - 2];
		System.arraycopy(LEFT_PANEL_COLLECTS, 1, PANEL_ID, 0,
				LEFT_PANEL_COLLECTS.length - 1);
		System
				.arraycopy(RIGHT_PANEL_COLLECTS, 1, PANEL_ID,
						LEFT_PANEL_COLLECTS.length - 1,
						RIGHT_PANEL_COLLECTS.length - 1);
		// 过滤出可以添加的Panel
		sysPanelConfigList = new ArrayList();
		for (int i = 0; i < rst.size(); i++) {
			HashMap panelMap = (HashMap) rst.get(i);
			String selectFlag = "false";
			for (int j = 0; j < PANEL_ID.length; j++) {
				// 如果此PANEL已被该用户选上
				if (panelMap.get("PANEL_ID").toString().equals(PANEL_ID[j])) {
					selectFlag = "true";
					break;
				}
			}
			if (selectFlag.equals("false")) {
				sysPanelConfigList.add(panelMap);
			}
		}

		// theForm.setPANEL_ID(PANEL_ID);
		theForm.setLEFT_PANEL_COLLECTS(LEFT_PANEL_COLLECTS);
		theForm.setRIGHT_PANEL_COLLECTS(RIGHT_PANEL_COLLECTS);
		theForm.setPANEL_COLLECTS(PANEL_COLLECTS);
		theForm.setPANEL_COLLECT(PANEL_COLLECT);

		return SUCCESS;
	}
	
	public String myportal(){
		TblSYS_LOGINMSG user = this.getCurrentUser();
		String login_id = user.getLOGIN_ID();

		HashMap map = sysUserPortalService
				.findObjTB_SYS_USER_PORTALByLoginID(login_id);

		String PANEL_COLLECT = "";
		// 如果用户配置了portal信息
		if (map != null) {
			PANEL_COLLECT = map.get("PANEL_COLLECT").toString();
		} else {
			// 如果没有定制,需要给参数初始化固定显示参数.
			PANEL_COLLECT = "0;1";
		}
		// 获取个人角色内所有portal,供用户进行定制,Start------
		String allPorals = "";
		String lastPortals = "";
		String singleRole = "";
		String comboRole = "";
		// 1.获取用户所具有的系统角色
		List roleList = sysUserRoleService.findRoleListByUserId(login_id);
		// 2.获取系统内所有角色所对应的portal
		List rolePortalList = sysRolePortalService.findRolePortalList();
		String[] ROLE_PORTAL_IDS = new String[300];
		// 3.portal角色与用户所拥有的系统角色进行对比,目的找出用户角色内所有的portal
		for (int i = 0; i < rolePortalList.size(); i++) {
			int sameSum = 0;
			int maxSameSum = 0;
			HashMap rolePortalMap = (HashMap) rolePortalList.get(i);
			// 3.1portal 角色可能有类似 1022#1050
			// 的组合.为了获取角色内所有的portal,这里进行分别获取,我感觉这里可以再优化
			String ROLE_IDS = rolePortalMap.get("ROLE_IDS").toString();
			ROLE_PORTAL_IDS = ROLE_IDS.split("#");
			// 3.11 if(单角色portal)else(多角色组合portal)
			if (ROLE_PORTAL_IDS.length == 1) {
				for (int m = 0; m < roleList.size(); m++) {
					TblSYS_USER_ROLE tblSYS_USER_ROLE = (TblSYS_USER_ROLE) roleList
							.get(m);
					if (ROLE_PORTAL_IDS[0]
							.equals(tblSYS_USER_ROLE.getROLE_ID())) {
						singleRole += rolePortalMap.get("PANEL_COLLECT")
								.toString();
						sameSum++;
					}
				}
				if (sameSum > maxSameSum) {
					maxSameSum = sameSum;
				}
				// 如果用户只有一种角色,且与角色portal 相匹配,则跳出循环
				if (maxSameSum == roleList.size()) {
					break;
				}
			} else {
				for (int j = 0; j < ROLE_PORTAL_IDS.length; j++) {
					for (int m = 0; m < roleList.size(); m++) {
						TblSYS_USER_ROLE tblSYS_USER_ROLE = (TblSYS_USER_ROLE) roleList
								.get(m);
						if (ROLE_PORTAL_IDS[j].equals(tblSYS_USER_ROLE
								.getROLE_ID())) {
							sameSum++;
						}
					}
					if (sameSum > maxSameSum) {
						maxSameSum = sameSum;
						comboRole = rolePortalMap.get("PANEL_COLLECT")
								.toString();
					}
					// 如果完全匹配，则跳出循环
					if (maxSameSum == roleList.size()) {
						break;
					}
				}
				// 如果完全匹配，则跳出循环
				if (maxSameSum == roleList.size()) {
					break;
				}
			}
		}
		// 4.获取到用户角色内所有portal的组合，可能有重复的portal,需要过滤一下。
		allPorals = singleRole + "," + comboRole;
		String[] portals = allPorals.split(",");
		List list = Arrays.asList(portals);
		Set set = new HashSet(list);
		Object[] obj = (Object[]) set.toArray();
		// 5. 过滤后,封装查询个人portal参数。
		for (int k = 0; k < obj.length; k++) {
			lastPortals += "'" + obj[k].toString() + "',";
		}
		lastPortals = lastPortals.substring(0, lastPortals.length() - 1);

		List rst = sysPanelConfigService
				.findSysPanelConfigListByRole(lastPortals);
		// 6.个人定制展现
		String[] PANEL_COLLECTS = PANEL_COLLECT.split(";");
		LEFT_PANEL_COLLECT = PANEL_COLLECTS[0];
		RIGHT_PANEL_COLLECT = PANEL_COLLECTS[1];
		String[] LEFT_PANEL_COLLECTS = LEFT_PANEL_COLLECT.split(",");
		String[] RIGHT_PANEL_COLLECTS = RIGHT_PANEL_COLLECT.split(",");
		String[] PANEL_ID = new String[LEFT_PANEL_COLLECTS.length
				+ RIGHT_PANEL_COLLECTS.length - 2];
		System.arraycopy(LEFT_PANEL_COLLECTS, 1, PANEL_ID, 0,
				LEFT_PANEL_COLLECTS.length - 1);
		System
				.arraycopy(RIGHT_PANEL_COLLECTS, 1, PANEL_ID,
						LEFT_PANEL_COLLECTS.length - 1,
						RIGHT_PANEL_COLLECTS.length - 1);
		// 过滤出可以添加的Panel
		sysPanelConfigList = new ArrayList();
		for (int i = 0; i < rst.size(); i++) {
			HashMap panelMap = (HashMap) rst.get(i);
			String selectFlag = "false";
			for (int j = 0; j < PANEL_ID.length; j++) {
				// 如果此PANEL已被该用户选上
				if (panelMap.get("PANEL_ID").toString().equals(PANEL_ID[j])) {
					selectFlag = "true";
					break;
				}
			}
			if (selectFlag.equals("false")) {
				sysPanelConfigList.add(panelMap);
			}
		}

		// theForm.setPANEL_ID(PANEL_ID);
		theForm.setLEFT_PANEL_COLLECTS(LEFT_PANEL_COLLECTS);
		theForm.setRIGHT_PANEL_COLLECTS(RIGHT_PANEL_COLLECTS);
		theForm.setPANEL_COLLECTS(PANEL_COLLECTS);
		theForm.setPANEL_COLLECT(PANEL_COLLECT);

		return SUCCESS;
	}
	
	public String savePortal(){
		HashMap<String,Boolean> result = new HashMap<String,Boolean>();
		try{
			TblSYS_LOGINMSG user = this.getCurrentUser();
			String login_id = user.getLOGIN_ID();
			String[] PANEL_ID = theForm.getPANEL_ID();
			String[] PANEL_COLLECTS = theForm.getPANEL_COLLECT().split(";");
			if (PANEL_ID != null && PANEL_ID.length > 0) {
				for (int i = 0; i < PANEL_ID.length; i++) {
					// 左右各加一次
					if (i % 2 == 0) {
						PANEL_COLLECTS[0] += "," + PANEL_ID[i];
					} else if (i % 2 == 1) {
						PANEL_COLLECTS[1] += "," + PANEL_ID[i];
					}
				}
			}
			String PANEL_COLLECT = PANEL_COLLECTS[0] + ";" + PANEL_COLLECTS[1];
			HashMap map = new HashMap();
			map.put("LOGIN_ID", login_id);
			map.put("PANEL_COLLECT", PANEL_COLLECT);
			map.put("SUBMIT_TIME", new Date());
			sysUserPortalService.deleteUserPortal(login_id);
			sysUserPortalService.add(map);
			theForm.setPANEL_ID(null);
			result.put("result", true);
		}catch(Exception ex){
			result.put("result", false);
		}
		flag = JSONUtil.toJSON(result);
		this.setJSON(flag);
		return SUCCESS;
	}
	
	public String userPortalConfig(){
		flag = "true";
		try{
			String PANEL_COLLECT = this.getRequest().getParameter("PANEL_COLLECT");
			TblSYS_LOGINMSG user = this.getCurrentUser();
			String login_id = user.getLOGIN_ID();
			// String login_id = CommUtil.USER_ADMIN;
			HashMap map = new HashMap();
			map.put("LOGIN_ID", login_id);
			map.put("PANEL_COLLECT", PANEL_COLLECT);
			map.put("SUBMIT_TIME", new Date());
			sysUserPortalService.deleteUserPortal(login_id);
			sysUserPortalService.add(map);
		}catch(Exception e){
			flag = e.getMessage();
		}

		return SUCCESS;
	}
	
	public String loadModuleListSon(){
		String nodeID = "";
		String rootID = "";
		String module_type = "";

		String MODULE_ID = this.getRequest().getParameter("MODULE_ID");
		module_type = this.getRequest().getParameter("module_type");
		String init = this.getRequest().getParameter("init");
		String path = this.getRequest().getParameter("path");
		TblSYS_LOGINMSG user = this.getCurrentUser();
		resultList = sysModuleService.findModuleListByLoginIDAndParentId(user.getLOGIN_ID(),
				MODULE_ID);
		

		nodeID = this.getRequest().getParameter("nodeID");
		rootID = this.getRequest().getParameter("rootID");


		return SUCCESS;
	}
	
	public String firstLevelTree(){
		String systemId = this.getRequest().getParameter("systemId");

		TblSYS_LOGINMSG user = this.getCurrentUser();
		resultList = sysModuleService
				.findModuleListByLoginIDAndParentId(user
						.getLOGIN_ID(), systemId);

		return SUCCESS;
	}
	
	public String userShortcutConfig() {
		flag = "true";
    	try{
			String MODULE_ID = this.getRequest().getParameter("MODULE_ID");
			TblSYS_LOGINMSG user = this.getCurrentUser();
			String login_id = user.getLOGIN_ID();
			HashMap map = new HashMap();
			map.put("LOGIN_ID", login_id);
			map.put("MODULE_ID", MODULE_ID);
			map.put("SUBMIT_TIME", new Date());
	
			sysUserShortcutService.add(map);
    	}catch(Exception e){
    		flag = e.getMessage();
    	}
                
		return SUCCESS;
	}

    public String addUserShortcut() {
    	flag = "true";
    	try{
            sysUserShortcutService.insertShortcut(getCurrentUser().getLOGIN_ID(), shortcut_name , shortcut_path,module_id);
    	}catch(Exception e){
    		flag = e.getMessage();
    	}
        return SUCCESS;
    }
        
	public String userShortcutUpdate(){
		flag = "true";
		try{
			String SHORTCUT_PATH = this.getRequest().getParameter("SHORTCUT_PATH");
			String SHORTCUT_NAME = java.net.URLDecoder.decode(this.getRequest()
					.getParameter("SHORTCUT_NAME"), "UTF-8");
	
			TblSYS_LOGINMSG user = this.getCurrentUser();
			String login_id = user.getLOGIN_ID();
	
			HashMap map = new HashMap();
			map.put("LOGIN_ID", login_id);
			map.put("SHORTCUT_PATH", SHORTCUT_PATH);
			map.put("SHORTCUT_NAME", SHORTCUT_NAME);
			sysUserShortcutService.update(map);
		}catch(Exception e){
    		flag = e.getMessage();
    	}
                
		return SUCCESS;
	}

	public String userShortcutDel() {
		flag = "true";
		try{
			sysUserShortcutService.deleteForTB_SYS_USER_SHORTCUT(this.getCurrentUser().getLOGIN_ID(),module_id);
		}catch(Exception e){
    		flag = e.getMessage();
    	}
		return SUCCESS;
	}
}
